package com.zklh.book.system.util;

import java.util.Random;

/**
 * id生成器
 *
 * @author 坐看落花
 * @version 1.0
 * @date 2021/3/20 23:04
 */
public class TokenGenerator {

    private static  final int DEFAULT_TOKEN_LENGTH = 8;

    private static final char[] CHAR_TABLE = "abcdefghijklmnopqrstuvwxyz123456789".toCharArray();

    private static final Random RANDOM = new Random(47);

    /**
     * 该生成方法可能会生成同一个token, 先这样写一个简单版本的生成器, 后续可以考虑更好的方案
     * @return
     */
    public  static String next(){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < DEFAULT_TOKEN_LENGTH; i++) {
            sb.append(CHAR_TABLE[RANDOM.nextInt(CHAR_TABLE.length)]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(next());
    }

}
